package com.lfg.leetcode.leetcode0;

import java.util.HashSet;
import java.util.Set;

public class No76minWindow {

    public static void main(String[] args) {
        String ret = minWindow("cabwefgewcwaefgcf", "cae");
        System.out.println("[" + ret + "]");
    }

    public static String minWindow(String s, String t) {
        String ret = "";
        if ("".equals(s) || null == s || "".equals(t) || null == t || t.length() > s.length()) {
            return ret;
        }
        int tl = t.length();
        Set<Character> condition = new HashSet<>(tl);
        Set<Character> data = new HashSet<>(tl);
        for (int i = 0; i < tl; i++) {
            condition.add(t.charAt(i));
        }
        int total = 0;
        int sl = s.length();
        int start = -1;

        for (int i = 0; i < sl; i++) {
            Character c = s.charAt(i);
            if (condition.contains(c)) {
                if (start == -1) {
                    start = i;
                }
                total++;
                data.add(c);
            }
            if (total >= tl && data.size() == condition.size()) {
                String current = s.substring(start, i + 1);
                if (current.length() <= ret.length() || "".equals(ret)) {
                    ret = current;
                }
                i = start;
                start = -1;
                data.clear();
                total = 0;
            }
        }
        return ret;
    }
}

